package player;

import java.awt.Color;
import java.awt.Point;

import player.Triangle;
import utilPackage.Explosion;

import main.Sprite;
import quadtree.DoublePoint;

public class Triangle extends Sprite {

	//public int x1, x2, y1, y2;
	public Color myColor;
	//public PointQuadTree<Sprite> spritesTree;
	private int counter = 0;
	public final static String sound = "../Sounds/guitar.wav";

	public Triangle(int x, int y, int width, int height, int dx) {
		super();
		myColor = Color.MAGENTA;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.dx = dx;
		this.dy = dx;
		//soundManager.setSound(sound);
	}
	
	public void die() {
		super.die();
		if(width <= 5){
			
			DoublePoint p = new DoublePoint(x, y);
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, 0, 2, 15));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, 2, 0, 15));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, 0, -2, 15));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, -2, 0, 15));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, 1, 1, 20));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, 1, -1, 20));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, -1, -1, 20));
			gameTree.insertSprite(p,new Explosion(getX() + (getWidth()/2), 
					getY() + (getHeight())/2, -1, 1, 20));
		}
		else {
			Triangle tmp1,tmp2,tmp3,tmp4;
			tmp1 = new Triangle(x, y, width/2,height/2, dx);
			tmp1.setGameTree(gameTree);
			tmp2 = new Triangle(x+ (width/2), y, width/2,height/2, dx);
			tmp2.setGameTree(gameTree);
			tmp3 = new Triangle(x, y + (height/2), width/2,height/2, dx);
			tmp3.setGameTree(gameTree);
			tmp4 = new Triangle(x+(width/2), y + (height/2), width/2,height/2, dx);
			tmp4.setGameTree(gameTree);
			gameTree.insertSprite(new DoublePoint(x,y), tmp1);
			gameTree.insertSprite(new DoublePoint(x+(width/2),y), tmp2);
			gameTree.insertSprite(new DoublePoint(x,y+(height/2)), tmp3);
			gameTree.insertSprite(new DoublePoint(x+(width/2),y+(height/2)), tmp4);
			tmp1 = tmp2 = tmp3 = tmp4 = null;
		}
	}

	public void draw(){

		g.setColor(myColor);
		g.fillRect(x, y, width, height);
		/*g.drawLine(x, y, x+width, y);
		g.drawLine(x, y, x, y+height);
		g.drawLine(x, y+height, x+width, y+height);
		g.drawLine(x+width, y, x+width, y+height);*/
	}
	public void think(){
		x+=dx;
		y+=dy;
		if(x + width >= 600) {
			x = 600 - width;
			dx = -dx;
		}
		if(y + height >= 675) {
			y = 675 - height;
			dy = -dy;
		}
		if(x <= 0) {
			x = 0;
			dx = -dx;
		}
		if(y<=0) {
			y = 0;
			dy = -dy;
		}
		counter++;
		if(counter > 50){
			counter = 0;
		}
		if(counter % 50 == 5) {
			
			int R = myRandom(220,255);
			int G = 0;
			int B = 0;
			int rand = myRandom(0,5);
			if(rand%5 == 1){
				myColor = new Color(G,R,B);
			}
			else if(rand%5 == 2){
				myColor = new Color(B,G,R);
			}
			else if(rand%5 == 3){
				myColor = new Color(R,B,G);
			}
			else if(rand%5 == 4){
				myColor = new Color(B,R,G);
			}
			else if(rand%5 == 5){
				myColor = new Color(G,B,R);
			}
			else {
				myColor = new Color(R,G,B);
			}
		}
	}
	
	public boolean pointWithin(int x, int y) {
		if(x >= this.x && x <= this.x + width && y >= this.y && y <= this.y + height) {
			return true;
		}
		return false;
	}

	public boolean doesCollide(Sprite s) {
		for(int i = y; i < y + height; i++) {
			for(int j = x; j < x + width; j++) {
				for(int si = s.getY(); si < s.getY() + s.getHeight(); si++) {
					for(int sj = s.getX(); sj < s.getX() + s.getWidth(); sj++) {
						if(i == si && sj == j) {
							dx = -dx;
							dy = -dy;
							//die();
							if(s instanceof Triangle) {
								s.setDx(-s.getDx());
								//soundManager.playAudio(sound);
								
								
								
							}
							else {
								s.die();
								die();
								
							}
							//s.setDy(-s.getDy());
							
							/*int R = myRandom(220,255);
							int G = 0;
							int B = 0;
							int rand = myRandom(0,5);
							if(rand%5 == 1){
								myColor = new Color(G,R,B);
								((Triangle)s).myColor = new Color(G,R,B);
							}
							else if(rand%5 == 2){
								myColor = new Color(B,G,R);
								((Triangle)s).myColor = new Color(B,G,R);
							}
							else if(rand%5 == 3){
								myColor = new Color(R,B,G);
								((Triangle)s).myColor = new Color(R,B,G);
							}
							else if(rand%5 == 4){
								myColor = new Color(B,R,G);
								((Triangle)s).myColor = new Color(B,R,G);
							}
							else if(rand%5 == 5){
								myColor = new Color(G,B,R);
								((Triangle)s).myColor = new Color(G,B,R);
							}
							else {
								myColor = new Color(R,G,B);
								((Triangle)s).myColor = new Color(R,G,B);
							}*/
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	public void run(){

		this.think();

	}

}
